/*
Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the length of last word in the string.

If the last word does not exist, return 0.

Note: A word is defined as a character sequence consists of non-space characters only.

For example,
Given s = "Hello World",
return 5.
*/

class Solution {
public:
    int lengthOfLastWord(const char *s) {
        int lastlen = 0, thislen = 0;
        while (*s) {
            if (*s++ != ' ') {
                thislen++;
            } else if (thislen) {
                lastlen = thislen;
                thislen = 0;
            }
        }
        return (thislen)?thislen:lastlen;
    }
};

#if 0
class Solution {
public:
    int lengthOfLastWord(const char *s) {
        if (!s) return 0;
        int len = strlen(s);
        if (!len) return 0;
        while (*(s + len -1) == ' ') len--; // remove tws
        if (!len) return 0; // all whitespaces
        int i = len -1;
        for (; i >= 0; i--) {
            if (s[i] == ' ') break;
        }
        return (len - i -1);
    }
};
#endif
